#include "Glow.h"

#include <iostream>

using namespace std;
using namespace glow;

int main(int argc, char **argv)
{
	// INITIALISATION
	GlowDevice *device = GlowDevice::getInstance();
	device->init(800, 600);
	
	Renderer *rdr = device->getRenderer();
	EventManager *emgr = device->getEventManager();
	
	// CHARGEMENT
	Image *img = new Image("../media/moon.jpg");
	Vector2i imgSize = Vector2i(rdr->getSize().x, rdr->getSize().y);
	img->setSize(Vector2i(imgSize.x, imgSize.y));
	img->setSize(Vector2i(64, 64));
	
	SkyBox sb;
	sb.load("../media/skybox/interZP.png",
		"../media/skybox/interZN.png",
		"../media/skybox/interYP.png",
		"../media/skybox/interYN.png",
		"../media/skybox/interXP.png",
		"../media/skybox/interXN.png");
	
	Camera c(Vector3d(15, 15, 5));
	c.setSensivity(1.0);
	c.setSpeed(2.0);

	bool quit = false;
	int angle = 0;
	
	bool show = false;
	device->showCursor(show);
	
	// MAIN LOOP
	while(!emgr->quit() && !quit)
	{
		/*if(imgSize.x != rdr->getSize().x || imgSize.y != rdr->getSize().y)
		{
			imgSize = Vector2i(rdr->getSize().x, rdr->getSize().y);
			img->setSize(Vector2i(imgSize.x, imgSize.y));
		}*/

		emgr->getEvent();
		c.getEvent();
		c.animate();
		sb.setPosition(c.getPosition());
		
		rdr->beginScene();
		c.look();
		sb.draw();

		rdr->set2DMode();
		img->draw();
		rdr->end2DMode();

		glDisable(GL_TEXTURE_2D);
		glDisable(GL_LIGHTING);
		
		glPushMatrix();
		glTranslated(0, 15, 0);
		glRotated(angle, 0, 0, 1);
		angle++;
		glColor3ub(255, 255, 255);
		glBegin(GL_QUADS);

		glColor3ub(255,0,0); //face rouge
		glVertex3d(1,1,1);
		glVertex3d(1,1,-1);
		glVertex3d(-1,1,-1);
		glVertex3d(-1,1,1);

		glColor3ub(0,255,0); //face verte
		glVertex3d(1,-1,1);
		glVertex3d(1,-1,-1);
		glVertex3d(1,1,-1);
		glVertex3d(1,1,1);

		glColor3ub(0,0,255); //face bleue
		glVertex3d(-1,-1,1);
		glVertex3d(-1,-1,-1);
		glVertex3d(1,-1,-1);
		glVertex3d(1,-1,1);

		glColor3ub(255,255,0); //face jaune
		glVertex3d(-1,1,1);
		glVertex3d(-1,1,-1);
		glVertex3d(-1,-1,-1);
		glVertex3d(-1,-1,1);

		glColor3ub(0,255,255); //face cyan
		glVertex3d(1,1,-1);
		glVertex3d(1,-1,-1);
		glVertex3d(-1,-1,-1);
		glVertex3d(-1,1,-1);

		glColor3ub(255,0,255); //face magenta
		glVertex3d(1,-1,1);
		glVertex3d(1,1,1);
		glVertex3d(-1,1,1);
		glVertex3d(-1,-1,1);
		
		glEnd();
		glPopMatrix();
		
		// Terrain plat
		glColor3ub(140,110,30);
		glScalef(10.0, 10.0, 10.0);
		glBegin(GL_QUADS);
		glVertex3d(-1,0,-1);
		glVertex3d(-1,0,1);
		glVertex3d(1,0,1);
		glVertex3d(1,0,-1);
		glEnd();
		
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_LIGHTING);
		
		rdr->endScene();

		if(emgr->isKeyDownOnce(GLFW_KEY_ESC)) quit = true;
		if(emgr->isKeyDownOnce('B'))
		{
			show = !show;
			device->showCursor(show);
		}
	}

	delete img;
	GlowDevice::destroy();
	
	return 0;
}
